package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.ServiceContent;
import org.dromara.system.domain.ServiceItem;
import org.dromara.system.domain.ServiceItemRecord;
import org.dromara.system.domain.TalentUser;
import org.dromara.system.domain.bo.ServiceItemRecordBo;
import org.dromara.system.domain.bo.SubmitServiceRequestBo;
import org.dromara.system.domain.vo.ServiceItemRecordVo;
import org.dromara.system.mapper.ServiceContentMapper;
import org.dromara.system.mapper.ServiceItemMapper;
import org.dromara.system.mapper.ServiceItemRecordMapper;
import org.dromara.system.mapper.TalentUserMapper;
import org.dromara.system.service.IServiceItemRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 服务事项记录Service业务层处理
 *
 * @author 毛宁川
 * @date 2025-07-23
 */
@Slf4j
@Service
public class ServiceItemRecordServiceImpl implements IServiceItemRecordService {

    @Autowired
    private ServiceItemRecordMapper baseMapper;

    @Autowired
    private TalentUserMapper talentUserMapper;

    @Autowired
    private ServiceContentMapper serviceContentMapper;

    @Autowired
    private ServiceItemMapper serviceItemMapper;

    /**
     * 查询服务事项记录
     *
     * @param serviceItemRecordId 主键
     * @return 服务事项记录
     */
    @Override
    public ServiceItemRecordVo queryById(Long serviceItemRecordId) {
        return baseMapper.selectVoById(serviceItemRecordId);
    }

    /**
     * 分页查询服务事项记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 服务事项记录分页列表
     */
    @Override
    public TableDataInfo<ServiceItemRecordVo> queryPageList(ServiceItemRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ServiceItemRecord> lqw = buildQueryWrapper(bo);
        Page<ServiceItemRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的服务事项记录列表
     *
     * @param bo 查询条件
     * @return 服务事项记录列表
     */
    @Override
    public List<ServiceItemRecordVo> queryList(ServiceItemRecordBo bo) {
        LambdaQueryWrapper<ServiceItemRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ServiceItemRecord> buildQueryWrapper(ServiceItemRecordBo bo) {
        LambdaQueryWrapper<ServiceItemRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ServiceItemRecord::getServiceItemRecordId);
        lqw.eq(bo.getUserId() != null, ServiceItemRecord::getUserId, bo.getUserId());
        lqw.like(bo.getUserName() != null, ServiceItemRecord::getUserName, bo.getUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhonenumber()), ServiceItemRecord::getPhonenumber, bo.getPhonenumber());
        lqw.eq(bo.getServiceItemId() != null, ServiceItemRecord::getServiceItemId, bo.getServiceItemId());
        lqw.like(StringUtils.isNotBlank(bo.getServiceItemName()), ServiceItemRecord::getServiceItemName, bo.getServiceItemName());
        lqw.eq(bo.getServiceContentId() != null, ServiceItemRecord::getServiceContentId, bo.getServiceContentId());
        lqw.eq(StringUtils.isNotBlank(bo.getServiceContentDesc()), ServiceItemRecord::getServiceContentDesc, bo.getServiceContentDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getTalentValue()), ServiceItemRecord::getTalentValue, bo.getTalentValue());
        lqw.like(StringUtils.isNotBlank(bo.getTalentName()), ServiceItemRecord::getTalentName, bo.getTalentName());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), ServiceItemRecord::getDeptName, bo.getDeptName());
        lqw.eq(bo.getDeptId() != null, ServiceItemRecord::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getServicePersonName()), ServiceItemRecord::getServicePersonName, bo.getServicePersonName());
        return lqw;
    }

    /**
     * 新增服务事项记录
     *
     * @param bo 服务事项记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ServiceItemRecordBo bo) {
        ServiceItemRecord add = MapstructUtils.convert(bo, ServiceItemRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setServiceItemRecordId(add.getServiceItemRecordId());
        }
        return flag;
    }

    /**
     * 修改服务事项记录
     *
     * @param bo 服务事项记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ServiceItemRecordBo bo) {
        ServiceItemRecord update = MapstructUtils.convert(bo, ServiceItemRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ServiceItemRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除服务事项记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public void submitService(SubmitServiceRequestBo bo) {
        ServiceItemRecord serviceItemRecord = new ServiceItemRecord();
        Long userId = bo.getUserId();
        if (userId == null) {
            throw new ServiceException("非法请求");
        }
        TalentUser talentUser = talentUserMapper.selectById(userId);
        if (talentUser == null) {
            throw new ServiceException("非法请求");
        }
        serviceItemRecord.setUserId(userId);
        serviceItemRecord.setTalentValue(talentUser.getNewFengjieTalentLevel());
        serviceItemRecord.setUserName(talentUser.getUserName());
        serviceItemRecord.setPhonenumber(talentUser.getPhonenumber());
        Long serviceContentId = bo.getServiceContentId();
        if (serviceContentId == null) {
            throw new ServiceException("非法请求");
        }
        ServiceContent serviceContent = serviceContentMapper.selectById(serviceContentId);
        ServiceItem serviceItem = serviceItemMapper.selectById(serviceContent.getServiceItemId());
        serviceItemRecord.setServiceContentId(serviceContentId);
        serviceItemRecord.setServiceItemId(serviceContent.getServiceItemId());
        serviceItemRecord.setServiceContentDesc(serviceContent.getServiceContentDesc());
        serviceItemRecord.setServiceItemName(serviceItem.getServiceItemName());
        baseMapper.insert(serviceItemRecord);

    }
}
